<!--
 * @Author: your name
 * @Date: 2020-08-12 09:06:21
 * @LastEditTime: 2020-08-12 21:08:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /topline-admin-yezi/src/views/addArticle/dialog.vue
-->
<template>
  <!-- dialog -->
  <el-dialog
    class="myDialog"
    :visible.sync="showDialog"
    :append-to-body="true"
    width="720px"
  >
    <el-tabs v-model="activePane_1" type="card">
      <el-tab-pane class="sec" label="素材库" name="source">
        <div class="hmTabs">
          <span
            :class="{ active: isAll }"
            @click="
              isAll = true
              getSourceList(1, isAll)
            "
            >全部</span
          >
          <span
            :class="{ active: !isAll }"
            @click="
              isAll = false
              getSourceList(1, isAll)
            "
            >收藏</span
          >
        </div>
        <div :class="['tab-item', { active: isAll }]">
          <div class="images">
            <div v-for="(item, i) in allSourceList" :key="i">
              <el-image
                :class="{ select: current === i }"
                :src="item.url"
                style="width: 100px; height: 100px"
                fit="fill"
                @click="onClick(i, item.url)"
              />
            </div>
          </div>
        </div>
        <div :class="['tab-item', { active: !isAll }]">
          <div class="images">
            <div v-for="(item, i) in collectSourceList" :key="i">
              <el-image
                :class="{ select: current === i }"
                :src="item.url"
                style="width: 100px; height: 100px"
                fit="fill"
                @click="onClick(i, item.url)"
              />
            </div>
          </div>
        </div>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="pagination.total"
          :page-size="10"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
        >
          >
        </el-pagination>
      </el-tab-pane>
      <el-tab-pane label="上传图片" name="upload">
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
          :headers="{ Authorization: `Bearer ${token}` }"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          name="image"
        >
          <img v-if="selectImage" :src="selectImage" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-tab-pane>
    </el-tabs>

    <span slot="footer" class="dialog-footer">
      <el-button
        @click="
          showDialog = false
          current = ''
          selectImage = ''
        "
        >取 消</el-button
      >
      <el-button
        type="primary"
        @click="
          $emit('got-image', selectImage)
          showDialog = false
          current = ''
          selectImage = ''
        "
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
// API
import { getSource } from '@/api/article'
import { getUser } from '@/utils/storage'

export default {
  created () {
    this.getSourceList(1, this.isAll)
  },
  computed: {
    token: function () {
      return getUser().token
    }
  },
  data () {
    return {
      isAll: true,
      showDialog: false,
      activePane_1: 'source',
      activePane_2: 'all',
      allSourceList: [],
      collectSourceList: [],
      current: '',
      selectImage: '',
      pagination: {
        total: null,
        currentPage: null
      }
    }
  },
  methods: {
    handleCurrentChange (page) {
      this.current = ''
      this.getSourceList(page, this.isAll)
    },
    onClick (index, url) {
      this.current = index
      this.selectImage = url
    },
    async getSourceList (page, is_collected, per_page = 10) {
      // 获取全部素材库
      const res = await getSource({
        is_collected,
        per_page,
        page
      })

      if (is_collected) {
        this.allSourceList = res.data.data.results
      } else {
        this.collectSourceList = res.data.data.results
      }

      this.pagination.currentPage = res.data.data.page
      this.pagination.total = res.data.data.total_count
    },
    handleAvatarSuccess (res, file) {
      this.selectImage = res.data.url
    },
    beforeAvatarUpload (file) {
      const isPass = ['image/jpeg'].includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isPass) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isPass && isLt2M
    }
  }
}
</script>

<style lang="less" scoped>
.myDialog {
  // upload 样式
  /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  /deep/.avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  /deep/.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  /deep/.avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  /deep/.sec {
    .el-tabs__heade {
      border-bottom: none;
    }
  }

  .sec {
    .hmTabs {
      display: flex;
      margin-bottom: 20px;

      span {
        color: #606266;
        font-size: 12px;
        border: 1px solid #dcdfe6;
        padding: 7px 15px;

        font-weight: 500;

        &:nth-child(1) {
          border-radius: 4px 0 0 4px;
          border-right: none;
        }
        &:nth-child(2) {
          border-radius: 0 4px 4px 0;
          border-left: none;
        }
        &.active {
          border: none;
          background-color: #66b1ff;
          color: white;
        }
      }
    }

    .tab-item {
      display: none;

      &.active {
        display: block;
      }

      .images {
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;

        > div {
          width: 15%;
          height: 15%;
          padding: 0 16px 16px 0;

          > div.select::before {
            position: absolute;
            top: 0;
            left: 0;
            content: '';
            display: block;
            width: 100%;
            height: 100%;
            background: url('../../assets/selected.png') rgba(0, 0, 0, 0.3)
              no-repeat center center;
            background-size: 50px 50px;
          }
        }
        > div:nth-child(5n) {
          padding-right: 0;
        }
      }
    }
  }
}
</style>
